package im.yisme.model;

public class LyricsAction {

	/*
	 * 类型定义
	 */
	//敲击类型
	public static final byte TAP = 0;
	//拖动类型
	public static final byte DRAG = 1;	
	
	/*
	 * 连接类型
	 */
	public static final byte BEGIN_POINT = 0;
	public static final byte LINK_POINT = 1;
	public static final byte END_POINT = 2;
	
	//手势类型
	private byte type;
	
	//触发时间点
	private int time;
	
	//触发点
	private byte point;
	
	//连接类型
	private byte connectionType;
	
	public LyricsAction(byte type, int time, byte point, byte connectionType) {
		super();
		this.type = type;
		this.time = time;
		this.point = point;
		this.connectionType = connectionType;
	}
	
	public LyricsAction(int actionInfo){
		binaryToAction(actionInfo);
	}
	
	public byte getType() {
		return type;
	}
	public void setType(byte type) {
		this.type = type;
	}
	
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
	
	public byte getPoint() {
		return point;
	}
	public void setPoint(byte point) {
		this.point = point;
	}
	
	public byte getConnectionType() {
		return connectionType;
	}

	public void setConnectionType(byte connectionType) {
		this.connectionType = connectionType;
	}

	/**
	 * 将Action数据转化为二进制数
	 * 顺序(3)T(1)CT(2)M(3)S(6)MS(10)
	 * @return
	 */
	public int toBinary(){
		int binary = 0;
		
		//触发点,3bit
		binary += point;
		
		//手势类型
		binary <<= 1;
		binary += type;
		
		//连接类型
		binary <<= 2;
		binary += connectionType;
		
		//触发时间
		int minute, second, millisecond;
		millisecond = time;
		second = millisecond / 1000;
		millisecond %= 1000;
		minute = second / 60;
		second %= 60;
		
		binary <<= 3;
		binary += minute;
		binary <<= 6;
		binary += second;
		binary <<=10;
		binary += millisecond;
		
		return binary;
	}
	
	/**
	 * 二进制数转化为Action数据
	 * @param actionInfo
	 */
	private void binaryToAction(int actionInfo){
		int millisecond = actionInfo & 0x000003ff;
		
		actionInfo >>= 10;
		int second = actionInfo & 0x0000003f;
		
		actionInfo >>= 6;
		int minute = actionInfo & 0x00000007;
		
		actionInfo >>= 3;
		connectionType = (byte)(actionInfo & 0x00000003);
		
		actionInfo >>= 2;
		type = (byte)(actionInfo & 0x00000001);
		
		actionInfo >>= 1;
		point = (byte)(actionInfo & 0x00000007);		

		time = millisecond + second * 1000 + minute * 60000;
	}
	
	public String toString(){
		StringBuffer string = new StringBuffer();
		
		string.append("二进制数据:" + toBinary());
		string.append("\n");
		
		string.append("手势类型:");
		if (type == TAP) string.append("敲击");
		if (type == DRAG) string.append("拖动");
		string.append("\n");
		
		string.append("连接类型:");
		if (connectionType == BEGIN_POINT) string.append("头");
		if (connectionType == LINK_POINT) string.append("连接点");
		if (connectionType == END_POINT) string.append("尾");
		string.append("\n");
		
		string.append("触发点:" + point);
		string.append("\n");
		
		string.append("触发时间：" + time + "毫秒");
		string.append("\n");
		
		return string.toString();
	}
}